// pages/aftersale/aftersale.js
const app =getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    filelength:4,
    imgurl:app.globalData.Imageurl,
    filelist:[],
    addshow:true,
    appraiseImag:'',
    fileimg:[],
    appraiseContent:'',
    refundExplain:'',
    array: ['选多了', '不想要了', '规格选错了', '太贵了','拍错商品'],
    index: 0,
    objectArray: [
      {
        id: 0,
        name: '选多了'
      },
      {
        id: 1,
        name: '不想要了'
      },
      {
        id: 2,
        name: '规格选错了'
      },
      {
        id: 3,
        name: '太贵了'
      },
      {
        id: 4,
        name: '拍错商品'
      }
    ],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      appraiseContent:this.data.array[0]
    })
    var sjs=this.randomWord()
    var datatime = Date.parse(new Date());
    var tokens=''+sjs+''+datatime+''+app.globalData.userId
    this.setData({
      token:tokens
    })
    this.setData({
      ordertype:options.ordertk
    })
    if(options.ordertk==1){
      this.getGoodsOrderDetails(options.orderId)
    }else{
      // console.log(options)
      this.setData({
        shoplist:JSON.parse(options.orderxq)
      })
      console.log(this.data.shoplist)
      // this.getGoodsOrderDetailsone(this.data.shoplist.id)
    }
   
  },
  bindblur(e){
    this.setData({
      refundExplain:e.detail.value 
    })
  },
  bindinput(e){
    this.setData({
      refundExplain:e.detail.value 
    })
  },
  randomWord() {
    var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
     var nums = "";
      for (var i = 0; i < 20; i++) {
      var id = parseInt(Math.random() * 61);
      nums += chars[id]
      }
      return nums; 
      },
    // 删除图片
      deleimg(e){
        var shuz=this.data.filelist
        console.log(shuz)
          shuz.splice(e.currentTarget.dataset.deleid,1)
          console.log(shuz)
          if (shuz.length<4) {
            this.setData({
              addshow:true,
            })
          }
          this.setData({
            filelist:shuz
          })
         
        console.log(e)
      },
      //选择图片
      fileup(){
        var t=this
         var countlegth= t.data.filelength-t.data.filelist.length
          
          wx.chooseImage({
            count: countlegth,
            sizeType: ['original', 'compressed'],
           sourceType: ['album', 'camera'],
           success (res) {
            // tempFilePath可以作为img标签的src属性显示图片
            console.log(res)
            const tempFilePaths = res.tempFilePaths
            t.setData({
              filelist:t.data.filelist.concat(tempFilePaths)
            })
            if (t.data.filelist.length<4) {
              t.setData({
                addshow:true,
              })
            }else{
              t.setData({
                addshow:false,
              })
            }
            }
          })
},
//联系客服
handleContact (e) {
  console.log(e.detail.path)
  console.log(e.detail.query)
},

  getGoodsOrderDetails(id){
    var t=this
    wx.request({
      url: app.globalData.Interfaceurl + 'getGoodsOrderDetails',
      method: "post",
      header: {
          "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
          userId: app.globalData.userId,
          orderId:id
      },
      success: res => {
          console.log(res)
          t.setData({
            goods:res.data.goodsOrder,
            consigneeAddress:res.data.goodsOrder.consigneeAddress,
            consigneeMobile:res.data.goodsOrder.consigneeMobile,
            consigneeName:res.data.goodsOrder.consigneeName,
          })
          console.log(t.data.goods)
          // if (myorderStats==''&&mypayStats=='') {
            
    
          // }
          
          
          
      }
  })
  },

  
  //申请售后
applyGoodsSale(){
  var t=this

  //有图片上传
if (t.data.appraiseContent!='') {
  wx.showLoading({
    title: '正在提交售后',
    mask:true
  })  

if (t.data.filelist.length>0) {
  t.setData({
    max:t.data.filelist.length
  })
  for(let i=0; i<t.data.filelist.length;i++){
    // t.saveFileUpload(t.data.filelist[i])
    
    wx.uploadFile({
        url: app.globalData.Interfaceurl + 'saveFileUpload',
        filePath: t.data.filelist[i],
        name: 'file',
        formData: {
            userId:app.globalData.userId,
        },
        header: { 
            "content-type": "multipart/form-data"
        },
        success (res){
          console.log(res)
            
            console.log(JSON.parse(res.data))
            t.setData({
              fileimg:t.data.fileimg.concat(JSON.parse(res.data).fileUrl)
            })
            
            
            if ((i+1)==t.data.max) {
              var appraiseImagstr =t.data.fileimg.toString()
              console.log(appraiseImagstr)
              wx.request({
                url: app.globalData.Interfaceurl + 'applyGoodsSale',
                method: "post",
                header: {
                    "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
                },
                data: {
                    userId: app.globalData.userId,
                    token:t.data.token,
                    goodsOrderItemId:t.data.shoplist.id,
                    refundReason:t.data.appraiseContent,
                    refundImages:appraiseImagstr,
                    refundExplain:t.data.refundExplain
                },
                success: res => {
                  console.log(res)
                  wx.hideLoading();
                  wx.navigateBack({
                    delta: 2,
                  })
                    
                }
              })
              
            }

         
        
        },
        fail (res){
          console.log(res)
        },
      })
  
  
   }
   
   //无图片上传
  }else{
    wx.request({
      url: app.globalData.Interfaceurl + 'applyGoodsSale',
      method: "post",
      header: {
          "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
          userId: app.globalData.userId,
          goodsOrderItemId:t.data.shoplist.id,
          refundReason:t.data.appraiseContent,
          token:t.data.token,
      },
      success: res => {
         console.log(res)
         
         wx.hideLoading();
         wx.navigateBack({
          delta: 2,
        })
      }
  })


  }
}else{
  wx.showToast({
    title: '请输入您的退款原因',
    icon:'none'
  })
}
},
bindPickerChange: function (e) {
  console.log('picker发送选择改变，携带值为', e)
  this.setData({
    index: e.detail.value
  })
  this.setData({
    appraiseContent:this.data.array[e.detail.value]
  })
  console.log(this.data.appraiseContent)
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})